<template>
    <div>
      <el-button type="primary" size="small" @click="isAdd = true">添加</el-button>
      
        <el-table :data="tableData" v-loading="isLoading">
      <el-table-column prop="id" label="id" />
      <el-table-column prop="name" label="配件名称" />
      <el-table-column prop="num" label="数量"  />
      <el-table-column prop="createTime" label="创建时间" />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="toAddNum(scope.row.id)"
            >进货</el-button
          >
          <el-button type="danger" size="mini" @click="del(scope.row.id)"
            >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      title="进货"
      :visible.sync="isAddNum"
      width="30%"
      @close="initSaveForm">
      <el-form :model="saveForm" ref="form"  label-width="80px" :inline="false" size="normal">
        <el-form-item label="数量">
          <el-input v-model="saveForm.num"></el-input>
        </el-form-item>
      </el-form>
      
      <span slot="footer">
        <el-button @click=" isAddNum = false">取消</el-button>
        <el-button type="primary" @click="save">确定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="添加"
      :visible.sync="isAdd"
      width="30%"
      @close="initAdd">
      <el-form :model="addForm" ref="form"  label-width="80px" :inline="false" size="normal">
        <el-form-item label="名称">
          <el-input v-model="addForm.name"></el-input>
        </el-form-item>
        <el-form-item label="数量">
          <el-input v-model="addForm.num"></el-input>
        </el-form-item>
      </el-form>
      
      <span slot="footer">
        <el-button @click=" isAdd = false">取消</el-button>
        <el-button type="primary" @click="add">添加</el-button>
      </span>
    </el-dialog>
    
    </div>
</template>
<script>
export default {
    name: 'Material',
    data() {
    return {
      isLoading: false,
      addForm:{
        name: '',
        num: '',
      },
      saveForm:{
        id: '',
        num: ''
      },
      isAdd: false,
      tableData: [],
      isAddNum: false,
    };
  },
  methods: {
    add() {
        this.postRequest("/material/add", this.addForm).then(()=>{
            this.isAdd = false
            this.getData()
        })
    },
    initAdd() {
      this.addForm = {
        name: '',
        num: '',
      }
    },
    del(id) {
      this.deleteRequest('/material/del/'+id).then(()=>{
        this.getData()
      })
    },
    toAddNum(id) {
      this.isAddNum = true
      this.saveForm.id = id
    },
    findName(arr, source, targetValue = "lable", sourceValue = "value") {
      if (!Array.isArray(arr)) {
        return;
      }
      let res = arr.find((e) => e[sourceValue] === source);
      return res && res[targetValue] ? res[targetValue] : "";
    },
    getData() {
      this.isLoading = true;
      
      this.postRequest("/material/list").then((resp) => {
        if (resp) {
          this.tableData = resp;
        }
        this.isLoading = false;
      });
    },
    
    initSaveForm() {
      this.saveForm = {
        id: '',
        num:  '',
      };
    },
  
    save() {
      if (this.saveForm.num == '' ) {
          this.$message.warning("请填写所有输入框");
          return;
      }
      if (!/^[1-9]\d*$/.test(this.saveForm.num)) {
          this.$message.warning("请输入数字");
          return;
      }
      
  
      this.postRequest("/material/addNum", this.saveForm).then(() => {
            this.isAddNum = false;
            this.getData();
            return;
        }
      )}
  },
  created() {
    this.getData();
    this.getRequest("/user/list/1").then((resp) => {
      this.repairUserList = resp;
    });
    this.getRequest("/user/list/2").then((resp) => {
      this.checkUserList = resp;
    });
  },
};
</script>
<style lang="css" scoped>
.isRed {
  color: red;
}
</style>
